package com.lungle.warmdiary.app.view.active.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.lungle.warmdiary.app.api.remote.ApiFactory;
import com.lungle.warmdiary.app.base.BaseActivity;
import com.lungle.warmdiary.app.base.BaseListFragment;
import com.lungle.warmdiary.app.base.ListBaseAdapter;
import com.lungle.warmdiary.app.model.response.Active;
import com.lungle.warmdiary.app.model.response.ActiveList;
import com.lungle.warmdiary.app.model.response.ActiveState;
import com.lungle.warmdiary.app.utils.UIHelper;
import com.lungle.warmdiary.app.view.active.adapter.ActiveAdapter;
import com.umeng.analytics.MobclickAgent;

import java.io.InputStream;
import java.io.Serializable;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class MyActiveFragment extends BaseListFragment<Active> {
    private static final String MY_ACTIVE_SCREEN = "my active screen";

    private int mState;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mState = getArguments().getInt("active_state", 0);
        int num = getArguments().getInt("active_num", 0);
        setActionTitle(mState, num);
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    private void setActionTitle(int state, int num) {
        String title;
        switch (state) {
            case ActiveState.STATE_UNCONFIRM:
                title = String.format("待确认(%s)", num);
                break;
            case ActiveState.STATE_UNSTART:
                title = String.format("未开始(%s)", num);
                break;
            case ActiveState.STATE_UNEXECUTE:
                title = String.format("待执行(%s)", num);
                break;
            case ActiveState.STATE_UNCHECK:
                title = String.format("待审核(%s)", num);
                break;
            case ActiveState.STATE_PASS:
                title = String.format("已通过(%s)", num);
                break;
            case ActiveState.STATE_UNPASS:
                title = String.format("未通过(%s)", num);
                break;
            case ActiveState.STATE_REFUSE:
                title = String.format("已拒绝(%s)", num);
                break;
            case ActiveState.STATE_ABANDON:
                if (num > 0) {
                    title = String.format("流单(%s)", num);
                } else {
                    title = "流单";
                }
                break;
            case ActiveState.STATE_STOP:
                title = String.format("叫停(%s)", num);
                break;
            default:
                title = "我的活动";
                break;
        }

        if (getActivity() != null && getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).setActionBarTitle(title);
        }

    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(MY_ACTIVE_SCREEN);
        MobclickAgent.onResume(getActivity());

    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(MY_ACTIVE_SCREEN);
        MobclickAgent.onPause(getActivity());

    }


    @Override
    protected ListBaseAdapter getListAdapter() {
        ActiveAdapter activeAdapter = new ActiveAdapter(ActiveAdapter.TYPE_NOSTATUS);
        return activeAdapter;
    }


    @Override
    protected ActiveList parseList(InputStream is) throws Exception {
        return ActiveList.parse(is);
    }


    @Override
    protected ActiveList readList(Serializable seri) {
        return (ActiveList) seri;
    }

    @Override
    protected void requestData(boolean refresh) {
        sendRequestData();
    }

    @Override
    protected void sendRequestData() {
        ApiFactory.getUserApi().getMyActiveList(mState, mCurrentPage * getPageSize(), getPageSize()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(mSubscriber);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Active item = mAdapter.getItem(position);
        if (item != null) {
//            UIHelper.showActiviDetail(getContext(), 2);
            UIHelper.showActiveDetail(getActivity(), item.getId(), item.getMediaid());
        }
    }


}
